package com.hopu.controller;

import com.hopu.pojo.Emp;
import com.hopu.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/emp")
@Scope("prototype")
public class EmpController {

    @Autowired
    private IEmpService iEmpService;

    @RequestMapping(value = "/findAll")
    public List<Emp> findAll() throws Exception {
        System.out.println("进入此方法");
        List<Emp> all = iEmpService.findAll();
        return all;
    }

    /**
     * 通过id查询
     * @param eid
     * @param did
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "findById")
    public Emp findById(@RequestParam(value = "eid",defaultValue = "")Integer eid,@RequestParam(value = "did",defaultValue = "") Integer did ) throws Exception {
        System.out.println("eid="+eid);
        System.out.println("did="+did);
        List<Emp> byId = iEmpService.findById(eid,did);
        Emp emp = byId.get(0);
        System.out.println(emp);
        System.out.println(emp.getDept());
        return emp;
    }

    @RequestMapping(value = "/update_emp")
    public int update_emp(@RequestParam(value = "eid",required = true)Integer eid,@RequestParam("ename")String ename) throws Exception {
        System.out.println(eid+"\t"+ename);
        int i = iEmpService.update_emp(eid,ename);
        return i;
    }

    @RequestMapping(value = "/save")
    public int save(@RequestParam(value = "ename",required = true)String ename,@RequestParam("did")Integer did) throws Exception {
        System.out.println(ename+"\t"+did);
        int i = iEmpService.save(ename,did);
        return i;
    }

    @RequestMapping(value = "/delete_emp")
    public int delete_emp(@RequestParam(value = "eid",required = true)Integer eid) throws Exception {
        System.out.println(eid);
        int i = iEmpService.delete_emp(eid);
        return i;
    }

}
